package javaapplication1;

/**
 * User: VU MINH KHOI
 * Date: April 4, 2011
 * CP1300 Assignment 1
 */
public class MainUI {

    private ProjModel pm;
    private GridView gv=null;

    public MainUI() {
        pm = new ProjModel();
    }

    public void showMenu() {
        int userChoiceInt=0;
        while(userChoiceInt <8){
            System.out.print( "\n === MENU ===\n" +
                    " 1. Set Number of horizontal lanes (H-street) [min 1, max 3]\n" +
                    " 2. Set Number of vertical lanes  (V-street) [min 1, max 4]\n" +
                    " 3. Set Probability of a car entering H-street [min 0, max 1]\n" +
                    " 4. Set Probability of a car entering V-street [min 0, max 1]\n" +
                    " 5. Run one simulation cycle\n" +
                    " 6. Set and run number of simulation cycles [min 1, max 10]\n" +
                    " 7. Quit \n" +
                    " ");
            String userChoice = GetUserInput.getUserChoice("Enter your choice>");
            userChoiceInt = Integer.parseInt(userChoice);
            switch(userChoiceInt) {
                case 1 :
                    System.out.println("You chose menu 1");
                    setNumHorLanes();
                    break;
                case 2 :
                    System.out.println("You chose menu 2");
                    setNumVerLanes();
                    break;
                case 3 :
                    System.out.println("You chose menu 3");
                    setProbHLanes();
                    break;
                case 4 :
                    System.out.println("You chose menu 4");
                    setProbVLanes();
                    break;
                case 5 :
                    System.out.println("You chose menu 5");
                    runCycle(1);
                    break;
                case 6 :
                    System.out.println("You chose menu 6");
                    runNumberOfSimulation();
                    break;
                case 7:
                    System.exit(0);
                    break;
                default :
                    System.out.println("Error in entering menu choice");
                    break;
            }
        }
    }

    public void setNumHorLanes () {
        String userChoice = GetUserInput.getUserChoice("Enter number of horiz lanes : ");
        int userChoiceInt = Integer.parseInt(userChoice);
        int lowerLimit = 1;
        int upperLimit = 3;
        if (userChoiceInt > upperLimit || userChoiceInt < lowerLimit) {
           System.out.println("Error in entering horiz lanes");
           System.out.println("Please enter number above 1 and below 3. ");
           showMenu();
        } else { //if user enter the correct input
            pm.setHorLanes(userChoiceInt);
            System.out.println("The number of horizontal lanes is " + userChoiceInt);
            //let user set the number
        }
    }

    public void setNumVerLanes () {
        String userChoice = GetUserInput.getUserChoice("Enter number of vert lanes : ");
        int userChoiceInt = Integer.parseInt(userChoice);
        int lowerLimit = 1;
        int upperLimit = 4;
        if (userChoiceInt > upperLimit || userChoiceInt < lowerLimit) {
           System.out.println("Error in entering horiz lanes");
           System.out.println("Please enter number above 1 and below 3. ");
           showMenu();
        } else { //if user enter the correct input
            pm.setVerLanes(userChoiceInt);
            System.out.println("The number of vertical lanes is " + userChoiceInt);
            //let user set the number
        }

    }

    public int showHorLanes() {
        return pm.getHorLanes();
    }

    public int showVerLanes() {
        return pm.getVerLanes();
    }

    private void setProbVLanes() {
        String userChoice = GetUserInput.getUserChoice("Enter probability of a car entering H-street: ");
        double userChoiceDbl = Double.parseDouble(userChoice);
        int lowerLimit = 0;
        int upperLimit = 1;
        if (userChoiceDbl > upperLimit || userChoiceDbl < lowerLimit) {
           System.out.println("Error in entering probaility");
           System.out.println("Please enter number above 0 and below 1. ");
           showMenu();
        } else { //if user enter the correct input
            pm.setProbVLanes(userChoiceDbl);
            System.out.println("The car probility on vertical lanes is " + userChoiceDbl);
            //let user set the number
        }
    }

    private void setProbHLanes() {
        String userChoice = GetUserInput.getUserChoice("Enter probability of a car entering V-street: ");
        double userChoiceDbl = Double.parseDouble(userChoice);
        int lowerLimit = 0;
        int upperLimit = 1;
        if (userChoiceDbl > upperLimit || userChoiceDbl < lowerLimit) {
           System.out.println("Error in entering probaility");
           System.out.println("Please enter number above 0 and below 1. ");
           showMenu();
        } else { //if user enter the correct input
            pm.setProbHLanes(userChoiceDbl);
            System.out.println("The car probility on horizontal lanes is " + userChoiceDbl);
            //let user set the number
        }
    }

    private void runCycle(int number) {
        if(this.checkBeforeSimulate()){
            if(gv == null)
                 gv = new GridView(this.pm);
            for(int i=0; i<number ; i++){
                gv.simulateACycle();
            }
        }
        else{
            System.out.println("You have to finish all menus from 1 to 4 to start simulating cycle");
            this.showMenu();
        }
    }

    private void runNumberOfSimulation() {
        if(this.checkBeforeSimulate()){
            String userChoice = GetUserInput.getUserChoice("Enter number of simulation cycles: ");
            int userChoiceInt = Integer.parseInt(userChoice);
            int lowerLimit = 1;
            int upperLimit = 10;
            if (userChoiceInt > upperLimit || userChoiceInt < lowerLimit) {
               System.out.println("Error in entering number");
               System.out.println("Please enter number above 1 and below 10. ");
               showMenu();
            } else { //if user enter the correct input
                this.runCycle(userChoiceInt);
                //let user set the number
            }
        }
        else
        {
            System.out.println("You have to finish all menus from 1 to 4 to start simulating cycle");
            this.showMenu();
        }
    }

    private boolean checkBeforeSimulate() {
        //check for settings before running simulation
        if(this.pm.getHorLanes()>0 && this.pm.getVerLanes()>0 && this.pm.getProbHLanes()>0 && this.pm.getProbVLanes()>0)
            return true;
        return false;
    }

    
}

